package com.senjoeson.teamfriends.adapter;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.senjoeson.teamfriends.R;
import com.senjoeson.teamfriends.activity.AddFriendActivity;
import com.senjoeson.teamfriends.bean.User;
import com.senjoeson.teamfriends.utils.GlideUtils;
import com.senjoeson.teamfriends.utils.MapUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;

public class NeigborAdapter extends RecyclerView.Adapter {


    private List<User> Datas;

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nearby_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        final User user = Datas.get(position);
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.mNickname.setText(user.getNickname());
        if ("男".equals(user.getGender())) {
            viewHolder.mGender.setBackgroundResource(R.mipmap.male);
        } else {
            viewHolder.mGender.setBackgroundResource(R.mipmap.female);
        }
        if (user.getPortrait() != null) {
            String fileUrl = user.getPortrait().getFileUrl();
            GlideUtils.loadImage(holder.itemView.getContext(), fileUrl, viewHolder.mPortrait);
        }
        viewHolder.mSign.setText(user.getSign());
        List<String> hobbies = user.getHobbies();
        StringBuilder addhobbies = new StringBuilder();
        if (hobbies != null) {
            for (String hobby : hobbies) {
                addhobbies.append(hobby);
            }
        }
        viewHolder.mUserinfo.setText(String.format("%s,%s,%s", user.getCity(), user.getSchool(), addhobbies.toString()));
        LatLng other = user.getLatLng();
        User currentUser = BmobUser.getCurrentUser(User.class);
        LatLng myUser = currentUser.getLatLng();
        String distance = "0 m";
        if (other != null && currentUser.getLatLng() != null) {
            distance = MapUtils.getLatLngDistance(other, myUser);
        }
        viewHolder.mDistance.setText(distance);
        viewHolder.mAddfocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userid = Datas.get(holder.getAdapterPosition()).getObjectId();
                Intent intent = new Intent(viewHolder.itemView.getContext(), AddFriendActivity.class);
                intent.putExtra("userid", userid);//用户id 可检测是否是我的好友
                intent.putExtra("username", user.getNickname());
                intent.putExtra("addfriendflag", user.isAddfriendflag());
                viewHolder.itemView.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return Datas != null ? Datas.size() : 0;
    }

    public void setData(List<User> list) {
        this.Datas = list;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.portrait)
        CircleImageView mPortrait;
        @BindView(R.id.nickname)
        TextView mNickname;
        @BindView(R.id.gender)
        ImageView mGender;
        @BindView(R.id.auth)
        ImageView mAuth;
        @BindView(R.id.userinfo)
        TextView mUserinfo;
        @BindView(R.id.sign)
        TextView mSign;
        @BindView(R.id.distance)
        TextView mDistance;
        @BindView(R.id.addfocus)
        TextView mAddfocus;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
